package com.jcl.recruit.api.common.controller;

import com.alibaba.fastjson.JSON;
import com.jcl.recruit.core.config.SystemConfig;
import com.jcl.core.utils.EncodeMd;
import com.jcl.recruit.user.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author 刘明新
 * @date 2018/8/13 下午4:48
 */
@RequestMapping("/api")
@RestController
public class ApiController {

    static Object o = new Object();

    @Autowired
    private UserService userService;

    /*@Autowired
    private AccessCloudClient accessCloudClient;*/

    /*@Resource
    private RedisTemplate<String, String> template;*/

    @Autowired
    StringRedisTemplate template;

    @RequestMapping(value = "/oaaccess")
    public void login(String loginid,String stamp,String token, HttpServletResponse response)throws IOException{
        Map<String,Object> map = new LinkedHashMap<>();
        String domain = SystemConfig.getSystemWebUrl();
        if(StringUtils.isBlank(loginid) || StringUtils.isBlank(stamp) || StringUtils.isBlank(token)){
            response.sendRedirect(domain + "/mb/console.html#/login");
            return;
        }
        String userToken = userService.responseToken(loginid);
        String localToken = EncodeMd.hexSHA1("oeLRb7" + loginid + stamp);
        if(localToken.equals(token)){
            response.sendRedirect(domain + "/mb/console.html#/?token="+userToken);
        }else{
            response.sendRedirect(domain + "/mb/console.html#/login");
        }
    }

    @RequestMapping("/resume/tooa")
    public Map<String,Object> resumeToOA(String token){
        Map<String,Object> map = new HashMap<>();
        if(StringUtils.isBlank(token) || userService.selectPrimaryKeyByToken(token) == null){
            map.put("type","error");
            map.put("message","还没有登录");
            map.put("errors","[ ]");
            return map;
        }
        map.put("type","success");
        map.put("message","执行成功");
        return map;
    }

    @RequestMapping("/spring/redis")
    public String testProvicer(String message){
        System.out.println("我要发送消息了");
        Map<String,String> params = new HashMap<>();
        params.put("keywords",message);
        params.put("test","dawfa");
        params.put("limin","liumingxin");
        template.convertAndSend("msg", JSON.toJSONString(params));
        /*try{
            System.out.println("消息正在发送...");
        }catch (Exception e){
            System.out.println("消息发送失败...");
        }*/
        return null;
    }
}
